<template>
	<view class="repair-wrap">
		<view class="" style='background: #fff;margin: 10px 10px 5px 10px;padding: 15px;'>
			<view>
				<image style="width: 60px;height: 60px;margin-bottom: -64px;"
					:src="orgImg || 'https://clhy-img.oss-cn-beijing.aliyuncs.com/homeActive.png' "></image>
				<view style="margin-left: 70px;margin-bottom: 15px;">
					<view class="" style='font-size: 18px;font-weight: bold;color: #333;padding: 0 0 15px 0;'>
						{{orgName || homelist.orgName}}
					</view>
					<view class="" style='display: flex;'>
						服务评分：<uni-rate :margin="8" disabled disabledColor="#F9A832" allow-half :size="18"
							active-color="#F9A832" v-model="orgScore" />{{orgScore}} 分
					</view>
				</view>
			</view>
		</view>
		<view class="" style='background: #fff;margin: 10px 10px 5px 10px;padding: 25px 25px 25px 15px;'>
			<view class="" style='font-size: 18px;font-weight: bold;padding: 0 0 15px 0;'>
				项目：{{homelist.proName}}
			</view>
			<view class="" style='display: flex;'>
				项目服务评分：<uni-rate :margin="8" disabled disabledColor="#F9A832" allow-half :size="18"
					active-color="#F9A832" v-model="proScore" />{{proScore}} 分
			</view>
		</view>

		<view class="" style='background: #fff;margin: 10px 10px 5px 10px;padding: 25px 25px 25px 15px;'>
			<view style="display: flex;">
				<view class="" style='font-size: 16px;font-weight: bold;color: #333;margin-right: 10px;'>
					我的项目评分
				</view>
				<view class="rep-deta-box-l rep-deta-box-l2" style='margin-top: 3px;'>
          <uni-rate @change="handleEvent" allow-half :margin="8" :size="18" active-color="#F9A832" v-model="value" />
				</view>
				<view style="color:#F9A832;margin-left: 5px;font-size: 14px;margin-top: 2px;" class="rep-deta-box-r" >
					{{value == 0?'':value < 2.5?'不满意':value < 4.5?'满意':value<=5?'非常满意':''}}
				</view>
			</view>
			<view style="color: #999999;margin-top:15px;">
				<text style="color: #000000;font-weight: 600;">温馨提示：</text><br>
				<view style="text-indent:2em;">
					只有业主本人可以对项目进行评分，每月只能进行一次评分，最后评分将覆盖历史评分。
				</view>
			</view>

		</view>
		<view style="margin-top: 30px;padding-bottom: 20px;" v-if="clickFlag == 0">
			<button class="yilingqu" @click='releaseBtn'>确认提交</button>
		</view>
	</view>
</template>

<script>
	import UniRate from "../../components/uni-rate/uni-rate";

	export default {
		components: {
			UniRate,

		},
		data() {
			return {
				orgImg: null,
				orgName: null,
				clickFlag: 0, //0.可以评分 1.不可
				nowTime: null, //当前时间
				lastTime: null, //最新评分时间
				proScore: null,
				orgScore: null,
				repairList: [],
				homelist: {},
				showFlag: false,
				name: false,
				value: '0',
				id: null,
			}
		},
		onShow() {
			this.getNowTime();
			let _this = this
			if (uni.getStorageSync('phone')) {
				if (uni.getStorageSync('loginFlag')) {
					if (!uni.getStorageSync('hourseList')) {
						this.$openInvite()
					} else {
						_this.showFlag = true
						_this.homelist = uni.getStorageSync('hourseList')[uni.getStorageSync('hourseNum') || 0]
						_this.name = _this.homelist.proName;
						_this.getScore();
						_this.getOrgInfo();
						_this.getOrgScore();
						_this.getProScore();
					}
				} else {
					uni.showModal({
						title: '请先实名认证',
						showCancel: false,
						confirmText: '确认',
						confirmColor: '#F9A832',
						success: function(res) {
							if (res.confirm) {
								_this.$Router.push({
									path: '/homePage1/realName/realName'
								})
							}
						}
					});
				}
			} else {
				uni.showModal({
					title: '请先登录',
					showCancel: false,
					confirmText: '确认',
					confirmColor: '#F9A832',
					success: function(res) {
						if (res.confirm) {
							console.log('a')
							_this.$Router.pushTab({
								path: '/pages/user/user'
							})
						}
					}
				});
			}
		},
		methods: {
      handleEvent(item) {
        this.value=item.value;
      },

			getNowTime() {
				var date = new Date(),
					year = date.getFullYear(),
					month = date.getMonth() + 1,
					day = date.getDate(),
					hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
					minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
					second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
				month >= 1 && month <= 9 ? (month = "0" + month) : "";
				day >= 0 && day <= 9 ? (day = "0" + day) : "";
				var timer = year + '-' + month
				this.nowTime = timer;

			},
			//获取该项目的评分
			getOrgScore() {
				this.$request.api.getPostitionScoreInfo({
					selectType: 1,
					id: this.homelist.proId,
					ownerOrgSelect: 1
				}).then(res => {
					if (res.data.data.score) {
						this.orgScore = res.data.data.score;
					} else {
						this.orgScore = 0
					}
				})
			},
			//获取该项目的评分
			getProScore() {
				this.$request.api.getPostitionScoreInfo({
					selectType: 2,
					id: this.homelist.proId
				}).then(res => {
					if (res.data.data.score) {
						this.proScore = res.data.data.score;
					} else {
						this.proScore = 0
					}
				})
			},
			getOrgInfo() {
				this.$request.api.getScoreOrgInfo({
					pro_id: this.homelist.proId,
				}).then(res => {
					if (res.data.data.orgName) {
						this.orgImg = res.data.data.orgImg;
						this.orgName = res.data.data.orgName;
					} else {
						this.orgImg = null;
					}
				})
			},
			getScore() {
				this.value = 0
				this.id = null
				this.$request.api.getScoreInfo({
					user_id: uni.getStorageSync('rawData').userId,
					room_id: this.homelist.roomId,
					pro_id: this.homelist.proId,
					score_type: 2,
				}).then(res => {
					if (res.data.data.scores) {
						this.value = res.data.data.scores;
						this.id = res.data.data.id;
						this.lastTime = res.data.data.update_time.substring(0, 7);
						//判断是在本月可以评分
						if (this.lastTime == this.nowTime) {
							this.clickFlag = 1;
						}
					} else {
						this.value = 0
						this.id = null
					}
				})
			},
			releaseBtn() {
				if (this.value < '0.5') {
					uni.showToast({
						title: '最低评分0.5分奥，请高于或等于0.5分~',
						duration: 2000,
						icon: 'none'
					});
					return
				}
				this.$request.api.addScoreRecord({
					user_id: uni.getStorageSync('rawData').userId,
					score_type: 2,
					pro_id: this.homelist.proId,
					scores: this.value,
					room_id: this.homelist.roomId,
					id: this.id ? this.id : null
				}).then(res => {
					if (res.data.code == 0) {
						uni.showToast({
							title: res.data.msg,
							duration: 2000,
							icon: 'none'
						});
						//返回上一页
						this.getScore();
						this.getProScore();
						this.getOrgScore();
					} else {
						uni.showToast({
							title: res.data.msg,
							duration: 2000,
							icon: 'none'
						});
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.repair-wrap {
		width: 100%;
		min-height: 100vh;
		height: auto;
		background: #f4f4f4;
		overflow: hidden;

		.repair-box {
			background: #fff;

			.repair-titel {
				height: 44rpx;
				line-height: 44rpx;

				.repair-titel-l {
					float: left;
				}

				.repair-titel-r {
					float: right;
					color: #999;
				}
			}

			.repair-6 {
				color: #666;
			}
		}
	}

	.yilingqu {
		width: 80%;
		height: 84rpx;
		background: linear-gradient(180deg, #76D904 0%, #417505 100%);
		border-radius: 44rpx;
		line-height: 84rpx;
		color: #FFFFFF;
		text-align: center;
		font-size: 32rpx;
	}
</style>